using RootMotion.FinalIK;
using UnityEngine;

public class KineticElementHit : KineticElementMove
{
	public delegate void OnHitDelegate(KineticElementHit hit);

	public OnHitDelegate onHitDelegate;

	public Vector3 hitPosition;

	public BallFlight trackFlight;

	public float hitTime;

	public float flightTimeWhenHit;

	private float animTime;

	public bool adjustHandToHitPosition = true;

	public float desiredAnimatorSpeed;

	public bool useIK;

	public float animatorSpeedAfterHit;

	public float normalizedReactionTime;

	private Vector3 prevRacketPos;

	private int frames;

	public HitAnimationInfo hitInfo
	{
		get
		{
			return animInfo as HitAnimationInfo;
		}
	}

	public FullBodyBipedIK ik
	{
		get
		{
			return character.GetComponent<FullBodyBipedIK>();
		}
	}

	protected Transform hitHandTransform
	{
		get
		{
			if (ik == null)
			{
				return null;
			}
			return ik.solver.GetLimbMapping(FullBodyBipedChain.RightArm).bone3;
		}
	}

	public override void OnStart(Transform character)
	{
		base.OnStart(character);
		animTime = 0f;
		frames = 0;
		desiredAnimatorSpeed = animatorSpeed;
		base.animator.speed = 1f;
	}

	protected override Vector3 AdjustPosition()
	{
		return base.AdjustPosition();
	}

	public override void OnLateUpdate()
	{
		if (base.animator == null)
		{
			return;
		}
		AnimatorStateInfo currentAnimatorStateInfo = base.animator.GetCurrentAnimatorStateInfo(0);
		animTime += GGGameTime.deltaTime * animatorSpeed / hitInfo.clipLength;
		if (trackFlight != null)
		{
			float num = hitInfo.hitTime / hitInfo.clipLength;
			float num2 = Mathf.Max(0f, hitInfo.hitTime - animTime);
			num2 = Mathf.Max(0f, flightTimeWhenHit - trackFlight.flightTime);
			float num3 = hitInfo.hitTime / hitInfo.clipLength - animTime;
			num3 = hitInfo.hitTime - currentAnimatorStateInfo.normalizedTime * hitInfo.clipLength;
			float num4 = Mathf.CeilToInt(num3 / GGGameTime.deltaTime);
			float num5 = Mathf.CeilToInt(num2 / GGGameTime.deltaTime);
			if (num2 > 0f)
			{
				float num6 = num3 * hitInfo.clipLength / num2;
				if (!(num5 > 0f) || num4 > 0f)
				{
				}
				if (num6 < 0.5f)
				{
					num6 = Mathf.Lerp(desiredAnimatorSpeed, num6 * 0.1f, base.animator.GetFloat("AdjustSpeedWeight"));
				}
				else if (num6 < 1f)
				{
					num6 = Mathf.Lerp(desiredAnimatorSpeed, num6, base.animator.GetFloat("AdjustSpeedWeight"));
				}
				else if (num6 >= 1f)
				{
					num6 = num6;
				}
				animatorSpeed = Mathf.Min(num6, 2f);
			}
			else
			{
				animatorSpeed = 1f;
			}
			Vector3 vector = character.Search("RUBBER001").position;
			Vector3 vector2 = trackFlight.PositionAt(trackFlight.flightTime - GGGameTime.deltaTime);
			Vector3 vector3 = trackFlight.PositionAt(trackFlight.flightTime);
			vector2 -= startRotation * Vector3.forward * 0.125f;
			vector3 -= startRotation * Vector3.forward * 0.125f;
			if (num2 - GGGameTime.deltaTime <= 0f && !base.animator.IsInTransition(0) && this.onHitDelegate != null)
			{
				OnHitDelegate onHitDelegate = this.onHitDelegate;
				this.onHitDelegate = null;
				trackFlight = null;
				onHitDelegate(this);
			}
			prevRacketPos = vector;
		}
		else if (animatorSpeedAfterHit > 0f)
		{
			animatorSpeed = animatorSpeedAfterHit;
		}
		else
		{
			animatorSpeed = desiredAnimatorSpeed;
		}
		frames++;
		base.animator.speed = animatorSpeed;
		Transform transform = hitHandTransform;
		if (!adjustHandToHitPosition)
		{
			if (ik != null)
			{
				ik.enabled = false;
			}
			return;
		}
		if (!useIK)
		{
			if (ik != null)
			{
				ik.enabled = false;
			}
			return;
		}
		if (ik != null)
		{
			ik.enabled = true;
		}
		Quaternion quaternion = startRotation;
		Vector3 vector4 = startPosition;
		Vector3 vector5 = vector4 + quaternion * hitInfo.hitPosition;
		Vector3 vector6 = Vector3.up * (hitPosition - vector5).y;
		Vector3 direction = Quaternion.Inverse(hitInfo.handRotationAtHit * quaternion) * vector6;
		Vector3 a = ((!(transform != null)) ? Vector3.zero : transform.TransformDirection(direction));
		float @float = base.animator.GetFloat("IKWeight");
		if (ik != null)
		{
			ik.solver.rightHandEffector.positionWeight = @float;
		}
		Vector3 vector7 = ((!(transform != null)) ? Vector3.zero : (transform.position + Vector3.Lerp(a, vector6, ScriptableObjectSingleton<KineticAnimationInfoDB>.instance.globalPosInfluenceWithIKWeight.Evaluate(@float))));
		if (ik != null)
		{
			ik.solver.rightHandEffector.position = vector7;
		}
	}

	public override void EndAnimation()
	{
		if (ik != null)
		{
			ik.enabled = false;
		}
		base.EndAnimation();
	}
}
